package com.mall.order.services;

import com.mall.commons.tool.exception.ValidateException;
import com.mall.order.OrderDetailQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.OrderDetailRequest;
import com.mall.order.dto.OrderDetailResponse;
import com.mall.order.dto.OrderItemDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * @author Connor
 * @date 2020/12/21
 * @Version 1.0
 */
@Slf4j
@Component
@Service(interfaceClass = OrderDetailQueryService.class)
public class OrderDetailQueryServiceImpl implements OrderDetailQueryService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderShippingMapper orderShippingMapper;

    @Autowired
    private OrderConverter orderConverter;

    @Override
    public OrderDetailResponse queryOrderDetail(OrderDetailRequest orderDetailRequest) {
        OrderDetailResponse orderDetailResponse = new OrderDetailResponse();

        try {
            orderDetailRequest.requestCheck();
            Example example = new Example(OrderItem.class);
            String orderId = orderDetailRequest.getOrderId();
            example.createCriteria().andEqualTo("orderId", orderId);
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            List<OrderItemDto> orderItemDtos = orderConverter.items2dto(orderItems);

            Order order = orderMapper.selectByPrimaryKey(orderId);
            orderDetailResponse.setOrderStatus(order.getStatus());
            orderDetailResponse.setOrderTotal(order.getPayment());
            orderDetailResponse.setUserId(order.getUserId());
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            orderDetailResponse.setTel(orderShipping.getReceiverMobile());
            orderDetailResponse.setStreetName(orderShipping.getReceiverAddress());
            orderDetailResponse.setUserName(orderShipping.getReceiverName());

            orderDetailResponse.setGoodsList(orderItemDtos);
            BigDecimal total = new BigDecimal(0);
            for (OrderItemDto orderItemDto : orderItemDtos) {
                total = total.add(orderItemDto.getTotalFee());
            }
            orderDetailResponse.setOrderTotal(total);
            orderDetailResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderDetailResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
            return orderDetailResponse;
        } catch (ValidateException e) {
            orderDetailResponse.setCode(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode());
            orderDetailResponse.setMsg(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
            return orderDetailResponse;
        }

    }
}
